const app = getApp();
Page({
  data: {
    PageCur: 'index',
  },
getUserInfo() {
  if (wx.getStorageSync('user')){
    return
  }
    wx.getUserInfo({
      success: res => {
        console.log("data",this.data)
        console.log(res.userInfo)
        this.data.user_img = res.userInfo.avatarUrl
        app.globalData.user_img = res.userInfo.avatarUrl
        app.globalData.user_name = res.userInfo.nickName
        this.setData({
          user: res.userInfo
        })
        wx.setStorageSync('user', res.userInfo)
        wx.login({
          success: function (r) {
            var code = r.code;//登录凭证
            console.log("loginaaa", app.globalData)
            if (code) {
              //2、调用获取用户信息接口
              wx.request({
                url: "http://" + app.globalData.url + "/cook/login",
                method: "POST",
                data : {"code": code, "user_img": app.globalData.user_img, "username": app.globalData.user_name},
                success: res => {
                  app.globalData.jwt = res.data.jwt
                  app.globalData.user_id = res.data.user_id
                  wx.setStorageSync('jwt_token', res.data.token)
                  wx.setStorageSync('user_id', res.data.user_id)
  
                }
              })
            }
          },
      
        })
        this.setData({
          T_F: "none"
        })
      }
    })

  },

onLoad: function(){
console.log("ccdeee")
console.log(wx.getStorageSync('user'))
console.log(wx.getStorageSync('user_id'))
if (wx.getStorageSync('user')){
  this.setData({
    T_F: "none"
  })
}
if (! wx.getStorageSync('user_id')){
  console.log("wx_logaaaa")
  wx.login({
    success: function (r) {
      var code = r.code;//登录凭证
      console.log("loginaaa", wx.getStorageSync('user').nickName)
      if (code) {
        //2、调用获取用户信息接口
        wx.request({
          url: "http://" + app.globalData.url + "/cook/login",
          method: "POST",
          data : {"code": code, "user_img": wx.getStorageSync('user').avatarUrl, "username": wx.getStorageSync('user').nickName},
          success: res => {
            console.log("resaaa", res)
            app.globalData.jwt = res.data.jwt
            app.globalData.user_id = res.data.user_id
            wx.setStorageSync('jwt_token', res.data.token)
            wx.setStorageSync('user_id', res.data.user_id)

          }
        })
      }
    },

  })
}
},

tipAgree:function(){
  console.log("hello world")
  //登录获取code
  //1、调用微信登录接口，获取code
  // wx.login({
  //   success: function (r) {
  //     var code = r.code;//登录凭证
  //     console.log("login", code)
  //     if (code) {
  //       //2、调用获取用户信息接口
  //       wx.request({
  //         url: "http://" + app.globalData.url + "/cook/login",
  //         method: "POST",
  //         data : {"code": code,},
  //         success: res => {
  //           app.globalData.jwt = res.data.jwt
  
  //         }
  //       })
  //     }
  //   },

  // })
  this.setData({
    T_F: "none"
  })
}
,
// 将弹窗设置为隐藏
untipAgree:function(){
  this.setData({
      T_F: "none"
  })
}
,
  toBasics(e) {
    this.setData({
      PageCur: 'basics'
    })
  },
  NavChange(e) {
    this.setData({
      PageCur: e.currentTarget.dataset.cur
    })
  },
  onShareAppMessage() {
    return {
      title: '口袋食谱',
      imageUrl: '/images/BasicsBg.png',
      path: '/pages/index/index'
    }
  },
  
})
